package com.guiji.quartz.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @create: 2021-09-26 17:41
 **/
@Component("attchImgMail")
public class AttchImgMail {
	private static final Logger logger = LoggerFactory.getLogger(AttchImgMail.class);
	private static final String sender = "gaoxiang@guiji.ai";

	@Autowired
	JavaMailSender javaMailSender;

	public void sendEmail() {
		MimeMessage mimeMailMessage = null;
		try {
			mimeMailMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
			mimeMessageHelper.setFrom(sender);
			mimeMessageHelper.setTo("svr8550@dingtalk.com");
			mimeMessageHelper.setCc("sunchengliang9731@dingtalk.com");
			mimeMessageHelper.setSubject("节点监控");
			mimeMessageHelper.setText("<html><body>Docker监控：<img src=\'cid:picture1\' >Mysql监控：<img src=\'cid:picture2\' >节点监控：<img src=\'cid:picture3\' ></body></html>", true);
			//文件路径
			MonitorSeleniumGifTask monitorSeleniumGifTask = new MonitorSeleniumGifTask();
			List<File> list = monitorSeleniumGifTask.monitorSeleniumGifTask();
			for (int i=1;i<4;i++) {
				FileSystemResource file = new FileSystemResource(list.get(i));
				//FileSystemResource file1 = new FileSystemResource(new File("src/main/resources/static/image/email.png"))
				//添加多个图片可以使用多条 <img src='cid:" + rscId + "' > 和 mimeMessageHelper.addInline(rscId, res) 来实现
				mimeMessageHelper.addInline("picture"+i, file);
			}
			javaMailSender.send(mimeMailMessage);
			System.out.println("===================发送成功====================");
		} catch (Exception e) {
			logger.error("邮件发送失败"+e);
		}
	}

}
